package com.chenglu.android.ordercomment.adapter;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chenglu.android.R;
import com.chenglu.android.bean.TabEvent;
import com.chenglu.android.common.utils.GlideEngine;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.Utils;
import com.chenglu.android.order.bean.OrderInfoBean;
import com.chenglu.android.ordercomment.OrderCommentActivity;
import com.chenglu.android.ordercomment.nine.PhotoEvent;
import com.chenglu.android.ordercomment.nine.SelectPlotAdapter;
import com.chenglu.android.ordercomment.nine.Tools;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lyj
 * @Created: by Copyright 2023/3/29 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：我的全部订单item
 */
public class CommentItemAdapter extends BaseQuickAdapter<OrderInfoBean.Data.Records.OrderDetails, BaseViewHolder> {
     OrderCommentActivity orderCommentActivity;

    public CommentItemAdapter(OrderCommentActivity context, int layoutResId, @Nullable List<OrderInfoBean.Data.Records.OrderDetails> data) {
        super(layoutResId, data);
        this.orderCommentActivity = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, OrderInfoBean.Data.Records.OrderDetails item) {
        helper.setText(R.id.tv_cart_goodsName, item.getGoodsVO().getGoodsName()); //商品名称

        if(item.getGoodsVO().getGoodsSkus().isEmpty()){
            helper.setVisible(R.id.tv_cart_goodsSpec,false);
        }else {
            helper.setVisible(R.id.tv_cart_goodsSpec,true);

             String datas = item.getGoodsVO().getGoodsSkus().get(0).getSpec().replace("{" ,"")
                     .replace("}","").replaceAll("\""," ");
//            Spec spec = com.alibaba.fastjson.JSONObject.parseObject(datas, Spec.class);

            helper.setText(R.id.tv_cart_goodsSpec,  datas); //规格
        }

         ImageView imageView = helper.getView(R.id.iv_cart_goodsImg);
         Glide.with(mContext).load(item.getGoodsVO().getGoodsImg()).into(imageView);

         //全部订单不显示
        helper.setVisible(R.id.tv_cart_goodsTotalPrice,true);

        TextView  tvmoney =  helper.getView(R.id.tv_cart_goodsTotalPrice);//显示商品价格

        tvmoney.setText("¥" +Utils.formatZeroNumber(item.getGoodsVO().getGoodsPrice()) );
//        Utils.goodsType(item.getGoodsVO().getGoodsType(),tvmoney,item.getGoodsVO().getGoodsPriceTotal());

        initAdapter(helper);
        //商品数量
        helper.setText(R.id.tv_cart_goodsNum, "" + "共"+item.getGoodsAmount()+"件");


        helper.setText(R.id.tv_order_sum, "" + "合计："+item.getGoodsVO().getGoodsPrice());

        RelativeLayout relativeLayout = helper.getView(R.id.rl_cart_gift);
        relativeLayout.setVisibility(View.GONE);//赠品不显示

        helper.addOnClickListener(R.id.rv_cart_goosImg); //点击事件

    }

    public SelectPlotAdapter adapter;
    public ArrayList<String> allSelectList;//所有的图片集合
    public ArrayList<String> categoryLists;//查看图片集合
    public List<LocalMedia> selectList = new ArrayList<>();

    public int mPhotoPosition;

    public int mDeletePhotoPosition;

    private void initAdapter(BaseViewHolder helper) {

        if (null == allSelectList) {
            allSelectList = new ArrayList();
        }
        if (null == categoryLists) {
            categoryLists = new ArrayList();
        }

        //最多9张图片
        RecyclerView  rvNinePhoto = helper.getView(R.id.rv_nine_photo);
        adapter = new SelectPlotAdapter(orderCommentActivity, 6);
        rvNinePhoto.setLayoutManager(new GridLayoutManager(orderCommentActivity, 3));
        adapter.setImageList(allSelectList);
        rvNinePhoto.setAdapter(adapter);
        adapter.setListener(new SelectPlotAdapter.CallbackListener() {
            @Override
            public void add(int position) {
                 mPhotoPosition = position;
                //可添加的最大张数=9-当前已选的张数
                int size = 6 - allSelectList.size();
                Tools.galleryPictures(orderCommentActivity, 1, size);

            }

            @Override
            public void delete(int position) {
                mDeletePhotoPosition = position;
                allSelectList.remove(position);
                categoryLists.remove(position);
                adapter.setImageList(allSelectList);//再set所有集合
            }

            @Override
            public void item(int position, List<String> mList) {
                selectList.clear();
                for (int i = 0; i < allSelectList.size(); i++) {
                    LocalMedia localMedia = new LocalMedia();
                    localMedia.setPath(allSelectList.get(i));
                    selectList.add(localMedia);
                }
                //①、图片选择器自带预览
                PictureSelector.create(orderCommentActivity)
                        .themeStyle(R.style.picture_default_style)
                        .isNotPreviewDownload(true)//是否显示保存弹框
                        .imageEngine(GlideEngine.createGlideEngine()) // 选择器展示不出图片则添加
                        .openExternalPreview(position, selectList);
                //②:自定义布局预览
                //Tools.startPhotoViewActivity(MainActivity.this, categoryLists, position);
            }
        });


    }


//    public String getPathFromLocalMedia(LocalMedia media) {
//        if (media == null || TextUtils.isEmpty(media.getPath())) {
//            return null;
//        }
//        String path;
//        if (media.isCut() && !media.isCompressed()) {
//            // 裁剪过
//            path = media.getCutPath();
//        } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
//            // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
//            path = media.getCompressPath();
//        } else {
//            // 原图地址
//            path = media.getPath();
//        }
//        return path;
//    }

    /**
     * 子类接受事件 重写该方法
     *
     * @param event 事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(TabEvent event) {
        int tab = event.getTab();
        LogUtils.d("ggggggggg");
        showSelectPic(orderCommentActivity.getMediaList());
//        showSelectPic(event.getLocalMediaList());

    }


    public void showSelectPic(List<LocalMedia> result) {
        LogUtils.e("size="+ result.size());
        for (int i = 0; i < result.size(); i++) {
            String path;
            //判断是否10.0以上
            if (Build.VERSION.SDK_INT >= 29) {
                path = result.get(i).getAndroidQToPath();
            } else {
                path = result.get(i).getPath();
            }
            allSelectList.add(path);
            categoryLists.add(path);
            LogUtils.e("图片链接: " + path);
        }
        adapter.setImageList(allSelectList);

    }

}
